<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI 文生图</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .input-section {
            margin-bottom: 20px;
        }
        textarea {
            width: 100%;
            height: 150px;
            margin: 10px 0;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            resize: vertical;
        }
        .model-section {
            margin-bottom: 20px;
        }
        select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            margin-bottom: 10px;
        }
        .button-group {
            display: flex;
            gap: 10px;
            justify-content: center;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:disabled {
            background-color: #cccccc;
            cursor: not-allowed;
        }
        button.secondary {
            background-color: #2196F3;
        }
        button:hover:not(:disabled) {
            opacity: 0.9;
        }
        .loading {
            display: none;
            text-align: center;
            margin: 20px 0;
        }
        .error {
            color: red;
            text-align: center;
            margin: 10px 0;
            display: none;
        }
        .result-section {
            margin-top: 20px;
            text-align: center;
        }
        .result-section img {
            max-width: 100%;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            margin-top: 10px;
        }
        .tips {
            background-color: #e8f5e9;
            padding: 15px;
            border-radius: 4px;
            margin: 20px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>AI 文生图</h1>
        
        <div class="tips">
            提示：请输入详细的图片描述，包含场景、风格、色调等信息，以获得更好的生成效果。
        </div>

        <div class="input-section">
            <label for="prompt">图片描述：</label>
            <textarea id="prompt" placeholder="请输入详细的图片描述..."></textarea>
        </div>

        <div class="model-section">
            <label for="modelSelect">选择模型：</label>
            <select id="modelSelect">
                <!-- 模型选项将通过 JavaScript 动态加载 -->
            </select>
        </div>

        <div class="button-group">
            <button onclick="generateImage()" id="generateBtn">生成图片</button>
            <button onclick="clearAll()" class="secondary">清空</button>
        </div>

        <div id="loading" class="loading">
            正在生成图片，请稍候...
        </div>

        <div id="error" class="error"></div>

        <div id="resultSection" class="result-section">
            <!-- 生成的图片将显示在这里 -->
        </div>
    </div>

    <script>
        // 页面加载时获取可用的模型列表
        document.addEventListener('DOMContentLoaded', loadImageModels);

        async function loadImageModels() {
            try {
                const response = await fetch('/available_image_models');
                if (response.ok) {
                    const data = await response.json();
                    const modelSelect = document.getElementById('modelSelect');
                    modelSelect.innerHTML = data.models.map(model => 
                        `<option value="${model.id}" title="${model.description}">${model.name}</option>`
                    ).join('');
                }
            } catch (err) {
                showError('加载模型列表失败: ' + err.message);
            }
        }

        async function generateImage() {
            const prompt = document.getElementById('prompt').value.trim();
            if (!prompt) {
                showError('请输入图片描述');
                return;
            }

            const model = document.getElementById('modelSelect').value;
            const generateBtn = document.getElementById('generateBtn');
            const loading = document.getElementById('loading');
            const resultSection = document.getElementById('resultSection');

            generateBtn.disabled = true;
            loading.style.display = 'block';
            hideError();
            resultSection.innerHTML = '';

            try {
                const response = await fetch('/generate_image/', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        prompt: prompt,
                        model: model
                    })
                });

                if (response.ok) {
                    const data = await response.json();
                    resultSection.innerHTML = `
                        <h3>生成结果：</h3>
                        <img src="${data.image_url}" alt="生成的图片">
                    `;
                } else {
                    const error = await response.json();
                    throw new Error(error.detail || error.error || '生成失败');
                }
            } catch (err) {
                showError(err.message);
            } finally {
                generateBtn.disabled = false;
                loading.style.display = 'none';
            }
        }

        function clearAll() {
            document.getElementById('prompt').value = '';
            document.getElementById('resultSection').innerHTML = '';
            hideError();
        }

        function showError(message) {
            const error = document.getElementById('error');
            error.textContent = message;
            error.style.display = 'block';
        }

        function hideError() {
            document.getElementById('error').style.display = 'none';
        }
    </script>
</body>
</html> 